$(function(){
    //个人会员注册切换功能
    $('#selReg').find('ul li').each(function(index){
        $(this).click(function(){
            $(this).addClass('active').siblings().removeClass('active');
            if(index == '0'){
                $('#emailReg').hide();
                $('#mobileReg').show();
            }else if(index == '1') {
                $('#emailReg').show();
                $('#mobileReg').hide();
            }
        });
    });

    //-------------------------------------------------------------------------------------------------------------------
    //个人会员手机号码验证
    $.validator.addMethod("checkUsername", function(value, element) {
		$('#emailForm').find("#username").val(str_trim(value));
		var regucn =/^(?=[\u4e00-\u9fa5])(?!\d+)[\u4e00-\u9fa5\w.]{2,20}$/;
        var reguen =/^(?=[\u4e00-\u9fa5a-zA-Z])(?!\d+)[\u4e00-\u9fa5\w.]{6,20}$/;
        if(regucn.test(str_trim(value))){
			return true;
        }else if(reguen.test(str_trim(value))){
			return true;
		}else{
            return false;
        }
    }, "中英文开头6-20位，无特殊符号");

    //个人会员邮箱表单验证 使用jQuery Validate 插件进行验证
    $('#emailForm').validate({
        //指定使用什么标签标记错误。
        errorElement: "span",
        //一个函数，可以自定义错误放到哪里。
        errorPlacement: function(error, element) {
            $(element).parents('dl').find(".errinfo").append(error);
        },
        //自定义错误消息
        tipPlacement: function(element,tip) {
            $(element).parents('dl').find(".errinfo").append(tip);
        },
        //通过验证后执行的函数
        success: function(label){
            label.text('ok!').addClass('success');
        },
        //未验证后执行的函数
        highlight: function(element, errorClass) {
            $( element ).closest( "dl" ).find("dd.errinfo").find('span').removeClass('success');
        },
        //js上面添加验证规则
        rules: {
            username: {
                required: true,
                minlength: 2,
                checkUsername:true, //期望的是true,如果为false则展示提示信息
                remote: {
                    url:'/personal/ajax_username',                    //后台处理程序
                    type: "post",               //数据发送方式
                    data: {                     //要传递的数据
                        username: function() { return $("#username").val();},
                        _token: _token
                    },
                    dataType: "html",
                    dataFilter: function(data, type) {
                        return (data == "true") ? true : false;
                    }
                }
            },
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 6
            },
            password_confirmation: {
                required: true,
                minlength: 6,
                equalTo: "#emailForm #password"
            },
            code: {
                required: true,
                remote: {
                    url:'/common/ajax_code',                //后台处理程序
                    type: "post",               //数据发送方式
                    data: {                     //要传递的数据
                        code: function() { return $("#code").val();},
                        _token: _token
                    },
                    dataType: "html",
                    dataFilter: function(data, type) {
                        return (data == "true") ? true : false;
                    }
                }
            },
            agree: "required"
        },
        //错误信息提示修改
        messages: {
            username: {
                required: "请输入用户名",
                minlength: "中英文开头6-20位，无特殊符号",
                remote: $.validator.format('用户名已经被注册，请重新输入')
            },
            email: {
                required: "请输入常用的邮箱",
                email: "请输入一个正确的邮箱"
            },
            password: {
                required: "请输入密码",
                minlength: "密码长度不能小于6个字符"
            },
            password_confirmation: {
                required: "请确认密码",
                minlength: "密码长度不能小于6个字符",
                equalTo: "两次密码输入不一致"
            },
            code: {
                required: "请输入验证码",
                remote: $.validator.format('验证码错误，请重新输入')
            },
            agree:"同意<<注册协议>>"
        },
        submitHandler:function(form) {
            form.submit();
        }
    });

    //-------------------------------------------------------------------------------------------------------------------

    //个人会员手机号码验证
    $.validator.addMethod("checkMobile", function(value, element) {
        var regu =/(^1[3-9]{1}[0-9]{9}$)/;
        if(regu.test(value)){
            return true;
        }else{
            return false;
        }
    }, "请输入正确的手机号码");

    //个人会员手机表单验证 使用jQuery Validate 插件进行验证
    $('#mobileForm').validate({
        //指定使用什么标签标记错误。
        errorElement: "span",
        //一个函数，可以自定义错误放到哪里。
        errorPlacement: function(error, element) {
            $(element).parents('dl').find(".errinfo").append(error);
        },
        //自定义错误消息
        tipPlacement: function(element,tip) {
            $(element).parents('dl').find(".errinfo").append(tip);
        },
        //通过验证后执行的函数
        success: function(label){
            label.text('ok!').addClass('success');
        },
        //未验证后执行的函数
        highlight: function(element, errorClass) {
            $( element ).closest( "dl" ).find("dd.errinfo").find('span').removeClass('success');
        },
        //js上面添加验证规则
        rules: {
            mobile: {
                required: true,
                minlength: 11,
                checkMobile:true, //期望的是true,如果为false则展示提示信息
                remote: {
                    url:'/personal/ajax_mobile',                 //后台处理程序
                    type: "post",               //数据发送方式
                    data: {                     //要传递的数据
                        mobile: function() { return $("#mobile").val();},
                        _token: _token
                    },
                    dataType: "html",
                    dataFilter: function(data, type) {
                        return (data == "true") ? true : false;
                    }
                }
            },
            mobCode: {
                required: true,
                remote: {
                    url:'/personal/ajax_sms_url',                //后台处理程序
                    type: "post",               //数据发送方式
                    data: {                     //要传递的数据
                        mobCode: function() { return $("#mobCode").val();},
                        mobile: function() { return $("#mobile").val();},
                        _token: _token
                    },
                    dataType: "html",
                    dataFilter: function(data, type) {
                        return (data == "true") ? true : false;
                    }
                }
            },
            password: {
                required: true,
                minlength: 6
            },
            password_confirmation: {
                required: true,
                minlength: 6,
                equalTo: "#mobileForm #password"
            },
            mobAgree: "required"
        },
        //错误信息提示修改
        messages: {
            mobile: {
                required: "请输入手机号码",
                minlength: "手机号码长度必须是11位数字",
                remote: $.validator.format('手机号码已经被注册，请重新输入')
            },
            mobCode: {
                required: "请输入验证码",
                remote: $.validator.format('验证码和手机号码不匹配！')
            },
            password: {
                required: "请输入密码",
                minlength: "密码长度不能小于6个字母"
            },
            password_confirmation: {
                required: "请确认密码",
                minlength: "密码长度不能小于6个字母",
                equalTo: "两次密码输入不一致"
            },
            mobAgree:"同意<<注册协议>>"
        },
        submitHandler:function(form) {
            form.submit();
        }
    });

    $('form input[placeholder]').placeholder({isUseSpan:true});
});

// ----------------------------------------------------------------------------------------------------------------------------

